package com.thinkgem.jeesite.modules.magnetic.util;

import com.thinkgem.jeesite.common.utils.DateUtils;
import com.thinkgem.jeesite.modules.area_info_his.entity.AreaInfoHis;
import com.thinkgem.jeesite.modules.area_info_his.service.AreaInfoHisService;
import com.thinkgem.jeesite.modules.area_info_yesterday.entity.AreaInfoYesterday;
import com.thinkgem.jeesite.modules.area_info_yesterday.service.AreaInfoYesterdayService;
import com.thinkgem.jeesite.modules.lane.entity.Lane;
import com.thinkgem.jeesite.modules.lane.service.LaneService;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

/**
 * @author xlp
 * @date 2019/4/12
 * 刷新各表的数据
 */
public class DataFlushUtil {

    @Autowired
    private LaneService laneService;

    @Autowired
    private AreaInfoYesterdayService areaInfoYesterdayService;

    @Autowired
    private AreaInfoHisService hisService;

    //将各个车道的剩余车位统计到对应的区域形成当天数据表
    public void flushAreaInfoYesterday(Lane lane){
        AreaInfoYesterday areaInfoYesterday = new AreaInfoYesterday();
        areaInfoYesterday.setAreaId(lane.getAreaId());
        //获取该区域下的所有车道的剩余车位总和
        areaInfoYesterday = laneService.getTotalRemainSpace(lane.getAreaId());
        areaInfoYesterdayService.save(areaInfoYesterday);
    }

    //处理某个区域里车位占用率,并存入历史数据表
    public static void handelAreaCarUse(List<AreaInfoYesterday> list, AreaInfoHisService hisService){
        //该区域总车位在24小时内的值(作为分母)
        long total = Long.valueOf(list.get(0).getTotalSpace()) * 24 * 3600;
        //该区域每个时段内的值之和(作为分子)
        long he = 0;
        //昨日日期
        Date date = new Date();
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        calendar.add(calendar.DATE,-1);
        date = calendar.getTime();

        //按每天24个时段统计每个时段的各区域车位占用率(在这里每天的凌晨时分必须要有一条记录)
        for (int hour=1; hour<=24; hour++){
            for (int i=0; i<list.size(); i++){
                //从第二条记录开始，因为第一条记录永远是凌晨时分
                if (i>0){
                    AreaInfoYesterday currentNode = list.get(i);     //当前节点
                    AreaInfoYesterday previousNode = list.get(i-1);   //上一个节点
                    //如果当前记录在当前时段内,则进行如下业务
                    if (currentNode.getTimePeriod().equals(String.valueOf(hour))){
                        System.out.println("当前遍历第"+(i+1)+"个节点");
                        System.out.println("当前时段："+hour);
                        System.out.println("当前节点处于时段："+currentNode.getTimePeriod()+" 时刻："+currentNode.getUpdateDate());
                        System.out.println("前一个节点处于时段："+previousNode.getTimePeriod()+" 时刻："+previousNode.getUpdateDate());
                        //如果上一个节点也在当前时段内,进行如下业务
                        if (previousNode.getTimePeriod().equals(String.valueOf(hour))){
                            long dateChange = ((currentNode.getUpdateDate().getTime() - previousNode.getUpdateDate().getTime()) / 1000);
                            System.out.println("当前节点与前一个节点的时间差为"+dateChange + "秒");
                            long spaceChange = Long.valueOf(currentNode.getTotalSpace()) - Long.valueOf(previousNode.getRemainSpace());
                            long cj = dateChange * spaceChange;
                            System.out.println("cj="+cj+" 需要相加的he="+he);
                            he += cj;
                            System.out.println("加完之后的he="+he);
                            //如果当前节点为该集合的最后一个节点,则将当前时段的占用率计算并录入历史表
                            if (i==list.size()-1){
                                AreaInfoHis his = DataFlushUtil.calculateCarUse(previousNode, he, total
                                        , date, String.valueOf(hour));
                                hisService.save(his);
                                he = 0l;
                            }
                        }else {     //如果上一个节点不在当前时段内,则进行如下业务
                            //获取上一个节点的时段位置(在逻辑上肯定是比当前节点的时段低)
                            String timePeriod = previousNode.getTimePeriod();
                            long spaceChange = Long.valueOf(currentNode.getTotalSpace()) - Long.valueOf(previousNode.getRemainSpace());
                            for (int j=Integer.valueOf(timePeriod); j<=hour; j++){
                                long dateChange = 0;
                                AreaInfoHis his = new AreaInfoHis();
                                if (j==Integer.valueOf(timePeriod)){
                                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd "+j+":00:00");
                                    dateChange = ((DateUtils.parseDate(sdf.format(date)).getTime() -
                                            previousNode.getUpdateDate().getTime()) / 1000);
                                    long cj = dateChange * spaceChange;
                                    he += cj;
                                    his = DataFlushUtil.calculateCarUse(previousNode, he, total, date, String.valueOf(j));
                                    //将变量"he"置空，重新计算下一时段
                                    he = 0l;
                                }
                                if(j>(Integer.valueOf(timePeriod))&&j<hour){
                                    dateChange = 3600;
                                    long cj = dateChange * spaceChange;
                                    his = DataFlushUtil.calculateCarUse(previousNode, cj, total, date, String.valueOf(j));
                                }
                                if (j==hour) {
                                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd "+(j-1)+":00:00");
                                    dateChange = ((currentNode.getUpdateDate().getTime() -
                                            DateUtils.parseDate(sdf.format(date)).getTime()) / 1000);
                                    long cj = dateChange * spaceChange;
                                    he += cj;
                                    break;
                                }
                                //录入历史表
                                hisService.save(his);
                            }
                        }
                    }
                }
            }
        }
    }

    //计算某一时段的车位占用率
    public static AreaInfoHis calculateCarUse(AreaInfoYesterday yesterday, long he,
                                              long total, Date date, String timePeriod){
        //计算该区域这一时段的的车位占用率
        System.out.println("时段为"+timePeriod+"的车位占有量总和为"+he);
        double rate = he/(double)total;
        String rateStr = String.format("%.2f", rate);
        System.out.println("该区域在时段为"+yesterday.getTimePeriod()
                +"的车位占有率为："+rateStr);
        AreaInfoHis his = new AreaInfoHis();
        his.setAreaId(yesterday.getAreaId());
        his.setAreaName(yesterday.getAreaName());
        his.setRate(rateStr);
        his.setTimePeriod(timePeriod);
        his.setTotalSpace(yesterday.getTotalSpace());
        //获取当前日期
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        his.setUpdateDate(DateUtils.parseDate(sdf.format(date)));
        return his;
    }

}
